package com.woniuxy.personal.core.controller;


import com.woniuxy.common.CommonResult;
import com.woniuxy.personal.core.model.PlOrderitem;
import com.woniuxy.personal.core.service.PlOrderService;
import com.woniuxy.personal.core.service.PlOrderitemService;
import com.woniuxy.personal.core.vo.OrderVo;
import com.woniuxy.personal.client.dto.PmEvaluate;
import com.woniuxy.personal.client.PmFeignClient;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author gmx
 * @since 2021-06-29
 */
@Api(tags = "订单相关接口")
@RestController
@RequestMapping("/order")
public class PlOrderController {
    @Resource
    private PlOrderitemService orderitemService;

    @Resource
    private PlOrderService orderService;
    @Resource
    private PmFeignClient pmFeignClient;

    /**
     * Description : 创建订单  <br/>
     * ChangeLog : 1. 创建 (2021/6/30 14:17 [binhu]);
     *
     * @param orderVo
     * @return com.woniuxy.common.CommonResult
     */
    @ApiOperation(value = "创建订单")
    @PostMapping("/create")
    public CommonResult createOrder(@RequestBody OrderVo orderVo) {
        String ordernumber = orderService.createOrder(orderVo);
        if (ordernumber == null) {
            return CommonResult.fail();
        }
        return CommonResult.success(ordernumber);
    }


    /**
     * Description : 查询我的订单
     * ChangeLog : 1. 创建 (2021/6/30 10:37 [fulang]);
     *
     * @param user_id
     * @return java.util.List<com.woniuxy.personal.core.vo.OrderVo>
     */
    @ApiOperation(value = "查询我的订单")
    @GetMapping("selectMyOrder/{user_id}")
    public CommonResult<List<OrderVo>> selectMyOrder(@PathVariable int user_id) {
        List<OrderVo> plOrders = orderService.selectOrder(user_id);
        for (OrderVo orderVo :
                plOrders
        ) {
            List<PlOrderitem> plOrderitems = orderitemService.selectOrderItem(orderVo.getId());
            orderVo.setPlOrderitems(plOrderitems);
        }
        return CommonResult.success(plOrders);
    }

    /**
     * Description : 查询待付款订单
     * ChangeLog : 1. 创建 (2021/6/30 11:29 [fulang]);
     *
     * @param user_id
     * @return com.woniuxy.common.CommonResult<java.util.List < com.woniuxy.personal.core.vo.OrderVo>>
     */
    @ApiOperation(value = "查询待付款订单")
    @GetMapping("selectNonpaymentOrderList/{user_id}")
    public CommonResult<List<OrderVo>> selectNonpaymentOrderList(@PathVariable int user_id) {
        List<OrderVo> plOrders = orderService.selectNonpaymentOrderList(user_id);
        for (OrderVo orderVo :
                plOrders
        ) {
            List<PlOrderitem> plOrderitems = orderitemService.selectOrderItem(orderVo.getId());
            orderVo.setPlOrderitems(plOrderitems);
        }
        return CommonResult.success(plOrders);
    }

    /**
     * Description : 查询待收货订单
     * ChangeLog : 1. 创建 (2021/6/30 11:29 [fulang]);
     *
     * @param user_id
     * @return com.woniuxy.common.CommonResult<java.util.List < com.woniuxy.personal.core.vo.OrderVo>>
     */
    @ApiOperation(value = "查询待收货订单")
    @GetMapping("selectPaidOrderList/{user_id}")
    public CommonResult<List<OrderVo>> selectPaidOrderList(@PathVariable int user_id) {
        List<OrderVo> plOrders = orderService.selectPaidOrderList(user_id);
        for (OrderVo orderVo :
                plOrders
        ) {
            List<PlOrderitem> plOrderitems = orderitemService.selectOrderItem(orderVo.getId());
            orderVo.setPlOrderitems(plOrderitems);
        }
        return CommonResult.success(plOrders);
    }

    /**
     * Description : 查询待服务订单
     * ChangeLog : 1. 创建 (2021/6/30 11:30 [fulang]);
     *
     * @param user_id
     * @return com.woniuxy.common.CommonResult<java.util.List < com.woniuxy.personal.core.vo.OrderVo>>
     */
    @ApiOperation(value = "查询待服务订单")
    @GetMapping("selectNonServiceOrderList/{user_id}")
    public CommonResult<List<OrderVo>> selectNonServiceOrderList(@PathVariable int user_id) {
        List<OrderVo> plOrders = orderService.selectNonServiceOrderList(user_id);
        for (OrderVo orderVo :
                plOrders
        ) {
            List<PlOrderitem> plOrderitems = orderitemService.selectOrderItem(orderVo.getId());
            orderVo.setPlOrderitems(plOrderitems);
        }
        return CommonResult.success(plOrders);
    }

    /**
     * Description : 查询待评价订单
     * ChangeLog : 1. 创建 (2021/6/30 11:30 [fulang]);
     *
     * @param user_id
     * @return com.woniuxy.common.CommonResult<java.util.List < com.woniuxy.personal.core.vo.OrderVo>>
     */
    @ApiOperation(value = "查询待评价订单")
    @GetMapping("selectNonAppraiseOrderList/{user_id}")
    public CommonResult<List<OrderVo>> selectNonAppraiseOrderList(@PathVariable int user_id) {
        List<OrderVo> plOrders = orderService.selectNonAppraiseOrderList(user_id);
        for (OrderVo orderVo :
                plOrders
        ) {
            List<PlOrderitem> plOrderitems = orderitemService.selectOrderItem(orderVo.getId());
            orderVo.setPlOrderitems(plOrderitems);
        }
        return CommonResult.success(plOrders);
    }

    /**
     * Description : 查询待售后订单
     * ChangeLog : 1. 创建 (2021/6/30 11:30 [fulang]);
     *
     * @param user_id
     * @return com.woniuxy.common.CommonResult<java.util.List < com.woniuxy.personal.core.vo.OrderVo>>
     */
    @ApiOperation(value = "查询待售后订单")
    @GetMapping("selectNonAfterServiceOrderList/{user_id}")
    public CommonResult<List<OrderVo>> selectNonAfterServiceOrderList(@PathVariable int user_id) {
        List<OrderVo> plOrders = orderService.selectNonAfterServiceOrderList(user_id);
        for (OrderVo orderVo :
                plOrders
        ) {
            List<PlOrderitem> plOrderitems = orderitemService.selectOrderItem(orderVo.getId());
            orderVo.setPlOrderitems(plOrderitems);
        }
        return CommonResult.success(plOrders);
    }

    /**
     * Description : 订单支付
     * ChangeLog : 1. 创建 (2021/6/30 14:15 [fulang]);
     *
     * @param id
     * @return com.woniuxy.common.CommonResult
     */
    @ApiOperation(value = "订单支付")
    @PutMapping("payOrder")
    public CommonResult payOrder(int id, int user_id) {
        int i = orderService.payOrder(id, user_id);
        if (i > 0) {
            return CommonResult.success();
        }
        return CommonResult.fail(500, "支付订单失败");
    }

    /**
     * Description : 确认收货
     * ChangeLog : 1. 创建 (2021/6/30 14:21 [fulang]);
     *
     * @param id
     * @return com.woniuxy.common.CommonResult
     */
    @ApiOperation(value = "确认收货")
    @PutMapping("confirm")
    public CommonResult confirm(int id, int user_id) {
        int confirm = orderService.confirm(id, user_id);
        if (confirm > 0) {
            return CommonResult.success();
        }
        return CommonResult.fail(500, "确认收货失败");
    }

    /**
     * Description : 取消订单
     * ChangeLog : 1. 创建 (2021/7/1 11:40 [fulang]);
     *
     * @param user_id
     * @param id
     * @return com.woniuxy.common.CommonResult
     */
    @ApiOperation(value = "取消订单")
    @DeleteMapping("cancelOrder")
    public CommonResult cancel(int user_id, int id) {
        int i = orderService.cancelOrder(user_id, id);
        if (i > 0) {
            return CommonResult.success();
        }
        return CommonResult.fail(500, "取消订单失败");
    }

    /**
     * Description :评价订单
     * ChangeLog : 1. 创建 (2021/7/2 10:52 [fulang]);
     *
     * @param evaluate
     * @return com.woniuxy.common.CommonResult
     */
    @ApiOperation(value = "评价订单")
    @PostMapping("/evaluate")
    public CommonResult insert(@RequestBody PmEvaluate evaluate) {

        CommonResult result = pmFeignClient.insert(evaluate);
        return result;
    }


    @GetMapping("selectOrderNumber/{id}")
    public String selectOrderNumber(@PathVariable("id") int id) {
        String orderNumber = this.orderService.selectOrderNumber(id);
        return orderNumber;
    }
}

